//
//  ViewController.swift
//  Genuine Discount
//
//  Created by Qingke on 16/5/16.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageController: UIPageControl!

    let pageWidth: CGFloat = 375
    let pageHeight: CGFloat = 128
    let disPlayName = ["photo-1","photo-2","photo-3","photo-4","photo-5"]
    var disPlayPicture: [String] = ["photo-1","photo-2","photo-3"]
    
    var scrollView1 = UIImageView()
    var scrollView2 = UIImageView()
    var scrollView3 = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.delegate = self
        initScrollView()
        initSubScrollView()
        addSubScrollViewImage()
    }
    
    func initScrollView() {
        scrollView.contentSize = CGSizeMake(pageWidth * 3, pageHeight)
        scrollView.pagingEnabled = true //是否支持分页
//        scrollView.showsHorizontalScrollIndicator = true //是否可以纵向拉伸
//        scrollView.showsVerticalScrollIndicator = true//是否可以横向拉伸
//        scrollView.scrollsToTop = true//点击状态栏回到顶部
//        scrollView.bounces = true
    }
    func initSubScrollView() {

        scrollView1 = UIImageView(frame: CGRectMake(0, 0, pageWidth, pageHeight))
        scrollView.addSubview(scrollView1)
        scrollView2 = UIImageView(frame: CGRectMake(pageWidth * CGFloat(1), 0, pageWidth, pageHeight))
        scrollView.addSubview(scrollView2)
        scrollView3 = UIImageView(frame: CGRectMake(pageWidth * CGFloat(2), 0, pageWidth, pageHeight))
        scrollView.addSubview(scrollView3)

    }
    func addSubScrollViewImage() {
        
        scrollView1.image = UIImage(named: disPlayPicture[0])
        scrollView2.image = UIImage(named: disPlayPicture[1])
        scrollView3.image = UIImage(named: disPlayPicture[2])
        scrollView.setContentOffset(CGPoint(x: pageWidth, y: 0), animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func updataDisPlayPicture(page: Int) {
        if page == 0 {
            disPlayPicture = [disPlayName[4],disPlayName[0],disPlayName[1]]

        } else if page == 4 {
            disPlayPicture = [disPlayName[3],disPlayName[4],disPlayName[0]]

        } else {
            disPlayPicture = [disPlayName[page - 1],disPlayName[page],disPlayName[page + 1]]

        }
        
    }
    func updataImageView() {
        
    }
}
extension ViewController: UIScrollViewDelegate {
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let currentPageDiff = scrollView.contentOffset.x / pageWidth
        print(currentPageDiff)
        print("1当前page: \(pageController.currentPage)")
        print("0当前偏移量： \(scrollView.contentOffset.x / pageWidth)")
        if scrollView.contentOffset.x / pageWidth >= 1 {
            //向左滑： 设置当前page
            if pageController.currentPage == 4 {
                pageController.currentPage = 0
            } else if pageController.currentPage == 3 {
                pageController.currentPage = 4
            } else {
                pageController.currentPage++
            }

        } else if scrollView.contentOffset.x / pageWidth <= -1 {
            //向右滑： 设置当前page
            if pageController.currentPage == 0 {
                pageController.currentPage = 4
            } else if pageController.currentPage == 1 {
                pageController.currentPage = 0
            }
        }
        
        print("2当前page: \(pageController.currentPage)")
        print("1当前偏移量： \(scrollView.contentOffset.x / pageWidth)")
        print(disPlayPicture)
        //通过pagecontrol 的当前页面设置scrollview 的3个view的图片
        //新增3个scrollview的图片
        addSubScrollViewImage()
        print("2当前偏移量： \(scrollView.contentOffset.x / pageWidth)")

    }
    
}

